home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / demo2.zoo / demo / ex / ex_tty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-31  |  6.7 KB  |  290 lines

  1. /*
  2.  * Copyright (c) 1980 Regents of the University of California.
  3.  * All rights reserved.  The Berkeley software License Agreement
  4.  * specifies the terms and conditions for redistribution.
  5.  */
  6.  
  7. #ifndef lint
  8. static char *sccsid = "@(#)ex_tty.c    7.11 (Berkeley) 3/9/87; 1.2 (Bellcore)    87/04/24";
  9. #endif not lint
  10.  
  11. #include "ex.h"
  12. #include "ex_tty.h"
  13.  
  14. /*
  15.  * Terminal type initialization routines,
  16.  * and calculation of flags at entry or after
  17.  * a shell escape which may change them.
  18.  */
  19. /* short    ospeed = -1;    mjm: def also in tputs.c of termcap.a  */
  20.  
  21. gettmode()
  22. {
  23.  
  24. #ifndef USG3TTY
  25.     if (gtty(1, &tty) < 0)
  26.         return;
  27.     if (ospeed != tty.sg_ospeed)
  28.         value(SLOWOPEN) = tty.sg_ospeed < B1200;
  29.     ospeed = tty.sg_ospeed;
  30.     normf = tty.sg_flags;
  31.     UPPERCASE = (tty.sg_flags & LCASE) != 0;
  32.     GT = (tty.sg_flags & XTABS) != XTABS && !XT;
  33.     NONL = (tty.sg_flags & CRMOD) == 0;
  34. #else
  35.     if (ioctl(1, TCGETA, (char *) &tty) < 0)
  36.         return;
  37.     if (ospeed != (tty.c_cflag & CBAUD))    /* mjm */
  38.         value(SLOWOPEN) = (tty.c_cflag & CBAUD) < B1200;
  39.     ospeed = tty.c_cflag & CBAUD;
  40.     normf = tty;
  41.     UPPERCASE = (tty.c_iflag & IUCLC) != 0;
  42.     GT = (tty.c_oflag & TABDLY) != TAB3 && !XT;
  43.     NONL = (tty.c_oflag & ONLCR) == 0;
  44. #endif
  45. }
  46.  
  47. char *xPC;
  48. char **sstrs[] = {
  49.     &AL, &BC, &BT, &CD, &CE, &CL, &CM, &xCR, &CS, &DC, &DL, &DM, &DO,
  50.     &ED, &EI, &F0, &F1, &F2, &F3, &F4, &F5, &F6, &F7, &F8, &F9,
  51.     &HO, &IC, &IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU, &LL, &ND, &xNL,
  52.     &xPC, &RC, &SC, &SE, &SF, &SO, &SR, &TA, &TE, &TI, &UP, &VB, &VS, &VE,
  53.     &AL_PARM, &DL_PARM, &UP_PARM, &DOWN_PARM, &LEFT_PARM, &RIGHT_PARM
  54. };
  55. bool *sflags[] = {
  56.     &AM, &BS, &DA, &DB, &EO, &HC, &HZ, &IN, &MI, &NC, &NS, &OS, &UL,
  57.     &XB, &XN, &XT, &XX
  58. };
  59. char **fkeys[10] = {
  60.     &F0, &F1, &F2, &F3, &F4, &F5, &F6, &F7, &F8, &F9
  61. };
  62. setterm(type)
  63.     char *type;
  64. {
  65.     char *tgoto();
  66.     register int unknown;
  67.     char ltcbuf[TCBUFSIZE];
  68.  
  69.     if (type[0] == 0)
  70.         type = "xx";
  71.     unknown = 0;
  72.     putpad(TE);
  73.     if (tgetent(ltcbuf, type) != 1) {
  74.         unknown++;
  75.         CP(ltcbuf, "xx|dumb:");
  76.     }
  77.     setsize();
  78.     aoftspace = tspace;
  79.     zap();
  80.     /*
  81.      * Initialize keypad arrow keys.
  82.      */
  83.     arrows[0].cap = KU; arrows[0].mapto = "k"; arrows[0].descr = "up";
  84.     arrows[1].cap = KD; arrows[1].mapto = "j"; arrows[1].descr = "down";
  85.     arrows[2].cap = KL; arrows[2].mapto = "h"; arrows[2].descr = "left";
  86.     arrows[3].cap = KR; arrows[3].mapto = "l"; arrows[3].descr = "right";
  87.     arrows[4].cap = KH; arrows[4].mapto = "H"; arrows[4].descr = "home";
  88.  
  89.     /*
  90.      * Handle funny termcap capabilities
  91.      */
  92.     if (CS && SC && RC) {
  93.         if (AL==NULL) AL="";
  94.         if (DL==NULL) DL="";
  95.     }
  96.     if (AL_PARM && AL==NULL) AL="";
  97.     if (DL_PARM && DL==NULL) DL="";
  98.     if (IC && IM==NULL) IM="";
  99.     if (IC && EI==NULL) EI="";
  100.     if (!GT) BT=NULL;    /* If we can't tab, we can't backtab either */
  101.  
  102. #ifdef TIOCLGET
  103.     /*
  104.      * Now map users susp char to ^Z, being careful that the susp
  105.      * overrides any arrow key, but only for hackers (=new tty driver).
  106.      */
  107.     {
  108.         static char sc[2];
  109.         int i;
  110.  
  111.         ioctl(0, TIOCGETD, (char *) &ldisc);
  112.         if (ldisc == NTTYDISC) {
  113.             sc[0] = olttyc.t_suspc;
  114.             sc[1] = 0;
  115.             if (olttyc.t_suspc == CTRL(z)) {
  116.                 for (i=0; i<=4; i++)
  117.                     if (arrows[i].cap &&
  118.                         arrows[i].cap[0] == CTRL(z))
  119.                         addmac(sc, (char *) NULL,
  120.                             (char *) NULL, arrows);
  121.             } else
  122.                 addmac(sc, "\32", "susp", arrows);
  123.         }
  124.     }
  125. #endif
  126.  
  127.     if (tgoto(CM, 2, 2)[0] == 'O')    /* OOPS */
  128.         CA = 0, CM = 0;
  129.     else
  130.         CA = 1, costCM = cost(tgoto(CM, 8, 10));
  131.     costSR = cost(SR);
  132.     costAL = cost(AL);
  133.     costDP = cost(tgoto(DOWN_PARM, 10, 10));
  134.     costLP = cost(tgoto(LEFT_PARM, 10, 10));
  135.     costRP = cost(tgoto(RIGHT_PARM, 10, 10));
  136.     PC = xPC ? xPC[0] : 0;
  137.     aoftspace = tspace;
  138.     CP(ttytype, longname(ltcbuf, type));
  139.     /* proper strings to change tty type */
  140.     termreset();
  141.     gettmode();
  142.     value(REDRAW) = AL && DL;
  143.     value(OPTIMIZE) = !CA && !GT;
  144.     if (ospeed == B1200 && !value(REDRAW))
  145.         value(SLOWOPEN) = 1;    /* see also gettmode above */
  146.     if (unknown)
  147.         serror("%s: Unknown terminal type", type);
  148. }
  149.  
  150. setsize()
  151. {
  152.     register int l, i;
  153.     char *getenv();
  154.  
  155.     if (strcmp(getenv("TERM"),"mgr") == 0) {        /* special MGR code */
  156.         int in = open("/dev/tty",0);
  157.         int out = open("/dev/tty",1);
  158.         char buff[20];
  159.         char *index();
  160.  
  161.         ex_sTTY(out);
  162.         if (options[FONT].ovalue != options[FONT].odefault) {
  163.             sprintf(buff,"\033%dF",options[FONT].ovalue);
  164.             write(out,buff,strlen(buff));
  165.             }
  166.         write(out,"\0332I",3);
  167.         read(in,buff,80);
  168.         COLUMNS = atoi(buff);
  169.         i = LINES = atoi(index(buff,' ')+1);
  170.         close(in);
  171.         close(out);
  172.         }
  173.     else {        /* normal terminal */
  174. #ifdef    TIOCGWINSZ
  175.     struct winsize win;
  176.  
  177.     if (ioctl(0, TIOCGWINSZ, (char *) &win) < 0) {
  178. #endif
  179.         i = LINES = tgetnum("li");
  180.         COLUMNS = tgetnum("co");
  181. #ifdef    TIOCGWINSZ
  182.     } else {
  183.         if ((LINES = winsz.ws_row = win.ws_row) == 0)
  184.             LINES = tgetnum("li");
  185.         i = LINES;
  186.         if ((COLUMNS = winsz.ws_col = win.ws_col) == 0)
  187.             COLUMNS = tgetnum("co");
  188.     }
  189. #endif
  190.     }
  191.     if (LINES <= 5)
  192.         LINES = 24;
  193.     if (LINES > TUBELINES)
  194.         LINES = TUBELINES;
  195.     l = LINES;
  196.     if (ospeed < B1200)
  197.         l = 9;    /* including the message line at the bottom */
  198.     else if (ospeed < B2400)
  199.         l = 17;
  200.     if (l > LINES)
  201.         l = LINES;
  202.     if (COLUMNS <= 4)
  203.         COLUMNS = 1000;
  204.     options[WINDOW].ovalue = options[WINDOW].odefault = l - 1;
  205.     if (defwind) options[WINDOW].ovalue = defwind;
  206.     options[SCROLL].ovalue = options[SCROLL].odefault = HC ? 11 : ((l-1) / 2);
  207.     if (i <= 0)
  208.         LINES = 2;
  209. }
  210.  
  211. zap()
  212. {
  213.     register char *namp;
  214.     register bool **fp;
  215.     register char ***sp;
  216.  
  217.     namp = "ambsdadbeohchzinmincnsosulxbxnxtxx";
  218.     fp = sflags;
  219.     do {
  220.         *(*fp++) = tgetflag(namp);
  221.         namp += 2;
  222.     } while (*namp);
  223.     namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullndnlpcrcscsesfsosrtatetiupvbvsveALDLUPDOLERI";
  224.     sp = sstrs;
  225.     do {
  226.         *(*sp++) = tgetstr(namp, &aoftspace);
  227.         namp += 2;
  228.     } while (*namp);
  229. }
  230.  
  231. char *
  232. longname(bp, def)
  233.     register char *bp;
  234.     char *def;
  235. {
  236.     register char *cp;
  237.  
  238.     while (*bp && *bp != ':' && *bp != '|')
  239.         bp++;
  240.     if (*bp == '|') {
  241.         bp++;
  242.         cp = bp;
  243.         while (*cp && *cp != ':' && *cp != '|')
  244.             cp++;
  245.         *cp = 0;
  246.         return (bp);
  247.     }
  248.     return (def);
  249. }
  250.  
  251. char *
  252. fkey(i)
  253.     int i;
  254. {
  255.     if (0 <= i && i <= 9)
  256.         return(*fkeys[i]);
  257.     else
  258.         return(NOSTR);
  259. }
  260.  
  261. /*
  262.  * cost figures out how much (in characters) it costs to send the string
  263.  * str to the terminal.  It takes into account padding information, as
  264.  * much as it can, for a typical case.  (Right now the typical case assumes
  265.  * the number of lines affected is the size of the screen, since this is
  266.  * mainly used to decide if AL or SR is better, and this always happens
  267.  * at the top of the screen.  We assume cursor motion (CM) has little
  268.  * padding, if any, required, so that case, which is really more important
  269.  * than AL vs SR, won't be really affected.)
  270.  */
  271. static int costnum;
  272. cost(str)
  273. char *str;
  274. {
  275.     int countnum();
  276.  
  277.     if (str == NULL || *str=='O')    /* OOPS */
  278.         return 10000;    /* infinity */
  279.     costnum = 0;
  280.     tputs(str, LINES, countnum);
  281.     return costnum;
  282. }
  283.  
  284. /* ARGSUSED */
  285. countnum(ch)
  286. char ch;
  287. {
  288.     costnum++;
  289. }
  290.